package yt.kpool.view.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import java.util.ArrayList;
import java.util.List;

import yt.kpool.R;
import yt.kpool.adapter.CurrencyListAdapter;
import yt.kpool.adapter.MinerLayoutSelectListviewAdapter;
import yt.kpool.bean.CurrencyList;
import yt.kpool.bean.CurrencyListBean;
import yt.kpool.util.DensityUtil;

/**
 * 币种选择
 *
 * @Author:Created by  ygy
 * @time:2019/10/10
 */
public class CurrencyPopupWindow extends PopupWindow {

    private Activity context;
    private View view;


    private OnSeleCurrencyLayoutListener onSeleCurrencyLayoutListener;
    private CurrencyListAdapter currencyListAdapter;

    private List<CurrencyList.DataBean> dataBean;

    public CurrencyPopupWindow(List<CurrencyList.DataBean> dataBean, Context context, OnSeleCurrencyLayoutListener onSeleCurrencyLayoutListener) {
        super(context);
        this.context = (Activity) context;
        this.dataBean = dataBean;
        this.onSeleCurrencyLayoutListener = onSeleCurrencyLayoutListener;
        initPop();

        initView();

    }

    private void initView() {
        View v = view.findViewById(R.id.view_null);


        GridView gridview = view.findViewById(R.id.gridview);
//        List<CurrencyListBean> list = new ArrayList<>();
//        list.add(new CurrencyListBean(1, "BTC", R.mipmap.upclick));
//        list.add(new CurrencyListBean(1, "LTC", R.mipmap.upclick));
//        list.add(new CurrencyListBean(1, "ETH", R.mipmap.upclick));
//        list.add(new CurrencyListBean(1,"GRIN-29",R.mipmap.upclick));


        currencyListAdapter = new CurrencyListAdapter(context, dataBean);
        gridview.setAdapter(currencyListAdapter);

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                onSeleCurrencyLayoutListener.selectHouseLayout(i);

                dismiss();
            }
        });
    }


    /**
     * 初始化pop样式
     */
    private void initPop() {

        view = LayoutInflater.from(context).inflate(R.layout.pop_currency_select_layout, null);
        int width = context.getWindowManager().getDefaultDisplay().getWidth();
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        int t=dataBean.size()%3>0?1:0;
        int s=(dataBean.size()/3+t)*70;
        int height=30+20*(dataBean.size()/3)+s;
        this.setHeight(DensityUtil.dip2px(context,height));
        this.setContentView(view);


        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.gray_bg2));
        //设置SelectPicPopupWindow弹出窗体的背景
        setBackgroundDrawable(dw);

        //获取popwindow焦点
        setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        setOutsideTouchable(true);

        //设置动画
        setAnimationStyle(R.style.AnimationPreview);

//        backgroundAlpha(0.6f);


        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }

        });
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        super.showAsDropDown(anchor, xoff, yoff);
        backgroundAlpha(0.6f);
    }

    /**
     * \
     * 设置透明背景
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }


    /**
     * 币种选择回调C
     */
    public interface OnSeleCurrencyLayoutListener {
        public void selectHouseLayout(int i);//不限

    }
}